const app = getApp()
Page({

  /**
   * 页面的初始数据
   * type 1:任务通知 2:官方通知
   */
  data: {
    isHidden: true,
    msg: '正在加载...',
    pageIndex: 1,
    pageTotal: 0,
    type: 1,
    accountList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    app.userLoginReadyCallback = res => {
      this.getIncomes(1);
    }
    this.getIncomes(1);
    
  },
  getIncomes(incomeType, page = 1) {
    var _this = this;

    wx.request({
      url: app.globalData.apiDomain + "crowdsource/wechat/getIncomList",
      header: { 'Content-Type': 'application/json', 'token': app.globalData.token },
      data: {
        incomeType: incomeType,
        pageNo: page
      },
      method: 'POST',

      success: function (res) {
        if (res.data.status === 'ok') {

          var account = res.data.object;
          console.log(account);
          if (account.pages >= _this.data.pageIndex) {

            console.log('xxx');
            _this.data.accountList = _this.data.accountList.concat(account.list);
            _this.data.pageTotal = account.pages;
            _this.setData({
              isHidden: true,
              msg: '正在加载...',
              accountList: _this.data.accountList
            });
          }
        } else{ 
          app.responseCheck(_this, res.data,'/pages/user/account/account')
        }
      },
      fail: function (e) {
        wx.showModal({
          content: '网络异常,请重试',
          showCancel: false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageIndex: 1,
      accountList: []
    });
    this.getIncomes(this.data.type);
    wx.stopPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

    console.log('onReachBottom');
    var _this = this;

    _this.setData({
      isHidden: false
    });

    if (_this.data.pageIndex >= _this.data.pageTotal) {
      setTimeout(function () {
        _this.setData({
          isHidden: true,
          msg: '达到底部'
        });
      }, 1000);
    } else {
      setTimeout(function () {
        _this.getIncomes(_this.data.type, ++_this.data.pageIndex);
      }, 1000);
    }
  },
  accountTab: function (event) {
    var _this = this;
    var type = event.target.dataset.type;
    this.setData({
      type: type
    });

    _this.setData({
      accountList: [],
      pageTotal: 0,
      pageIndex: 1
    });
    this.getIncomes(type);
  },
  makeMoney: function (e) {
    wx.switchTab({
      url: '../../public_tasks/task_list/task_list',
    })
  }
})